xtset firm trend
preserve
*forvalues firm=1/110 {
*quietly rolling in 1/13200, window(12) clear noisily : reg cds_return sentiment mktrf smb hml, vce( cluster firm) 
*}

rolling in 1/13200, window(12) clear noisily : reg cds_return sentiment leverage liquidity, robust
rename _b_cons excess_return
reg excess_return _b_sentiment _b_leverage _b_liquidity _b_mktrf _b_smb _b_hml, be

sort _b_sentiment
gen portfolio = int((mod(_n-1, 11990))/2997) +1 // add last two rows 4&4

bysort portfolio: su excess_return _b_leverage _b_liquidity

ttest excess_return if portfolio==1 | portfolio==4, by(portfolio)

*------ to sort on sentiment rather than in Beta of sentiment -------
*preserve
*sort sentiment
*gen portfolio = int((mod(_n-1, 13200))/3300) +1
*bysort portfolio: su cds_return leverage  
* **Estimate annualized mean returns**
bysort portfolio: egen mean_return = mean(excess_return)
gen annual_mean_return = mean_return * 12 
*_______________________________________________________________________________
* **Estimate annualized standard deviation**
bysort portfolio: egen sd_return = sd(excess_return)
gen annual_mean_return = mean_return * 12
gen annual_sd_return = sd_return * sqrt(12)
*-------------------------------------------------------------------------------
* Get the t-stats of the Portfolios Summarize data by portfolio
*egen mean_return = mean(excess_return), by(portfolio)
egen std_dev = sd(excess_return), by(portfolio)
egen n_obs = count(excess_return), by(portfolio)

* Calculate t-statistics for each portfolio
gen t_stat = mean_return / (std_dev / sqrt(n_obs))

bysort portfolio: su t_stat

* **Calculate Sharpe Ratio**
bysort portfolio: gen sharpe_ratio = annual_mean_return / annual_sd_return

* **Estimate downside deviation**
gen downside_deviation = (excess_return < 0) * excess_return 
bysort portfolio: egen dd_sd = sd(downside_deviation) 
gen annual_dd_sd = dd_sd * sqrt(12)

* **Calculate Sortino Ratio**
bysort portfolio: gen sortino_ratio = annual_mean_return / annual_dd_sd

* **Display results**
preserve
collapse (mean) sharpe_ratio sortino_ratio, by(portfolio)
list 
restore

* ------------------ Equity Excess Returns --------------------------

xtset firm trend
preserve
*forvalues firm=1/110 {
*quietly rolling in 1/13200, window(12) clear noisily : reg cds_return sentiment mktrf smb hml, vce( cluster firm) 
*}

rolling in 1/13200, window(12) clear noisily : reg equity_return rj_equity mktrf smb hml, robust
predict fitted
drop if firm==1
sort _b_rj_equity 
gen portfolio = int((mod(_n-1, 11880))/2970) +1
rename _b_cons excess_return
ttest excess_return if portfolio==1 | portfolio==4, by(portfolio)

* **Estimate annualized mean returns**
bysort portfolio: egen mean_return = mean(excess_return)
gen annual_mean_return = mean_return * 12 

* **Estimate annualized standard deviation**
bysort portfolio: egen sd_return = sd(excess_return)
gen annual_sd_return = sd_return * sqrt(12)

*-------------------------------------------------------------------------------
* Get the t-stats of the Portfolios Summarize data by portfolio
egen mean_return = mean(excess_return), by(portfolio)
egen std_dev = sd(excess_return), by(portfolio)
egen n_obs = count(excess_return), by(portfolio)

* Calculate t-statistics for each portfolio
gen t_stat = mean_return / (std_dev / sqrt(n_obs))

bysort portfolio: su t_stat

* **Calculate Sharpe Ratio**
bysort portfolio: gen sharpe_ratio = annual_mean_return / annual_sd_return

* **Estimate downside deviation**
gen downside_deviation = (excess_return < 0) * excess_return 
bysort portfolio: egen dd_sd = sd(downside_deviation) 
gen annual_dd_sd = dd_sd * sqrt(12)

* **Calculate Sortino Ratio**
bysort portfolio: gen sortino_ratio = annual_mean_return / annual_dd_sd

* **Display results**
preserve
collapse (mean) sharpe_ratio sortino_ratio, by(portfolio)
list 
restore

